package com.dhcc.DRGsHosp.web.controller;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.DRGsHosp.facade.EsRestTempFacade;

/**
 * @Discription：ES创建es索引    
 * @Author：liuhongjun  
 * @ClassName: EsRestTemplateController  
 * @Date: 2018年5月10日 下午9:58:29  
 * @Version：3.1.0 DHC.DRGs
 */
@Controller
@RequestMapping("/EsRestTemplate")
public class EsRestTemplateController {
	@Autowired
	private  EsRestTempFacade  esRestTempFacade;
	/**
	 *  @Discription：创建ES索引
	 *  @Author： liuhongjun
	 *  @param method
	 *  @return
	 *  @Date:2018年5月11日上午10:39:23
	 */
	@RequestMapping("/createEsIndex")
	@ResponseBody
	public InvokeResult createEsIndex(String esIndex) {
		try {
			Map<String,Object> map=getEsInfo(esIndex);
			//引名称
		//	String esIndex = map.get("ES_INDEX")==null?"":map.get("ES_INDEX").toString();
			//服务器ip
			String esServerIps =map.get("ES_SERVERIP")==null?"":map.get("ES_SERVERIP").toString();
			//ES服务端口，默认9200
			String esServerPorto =map.get("ES_CREATE_PORT")==null?"":map.get("ES_CREATE_PORT").toString();
			//es索引字段
			String filed =map.get("ES_FILDES")==null?"":map.get("ES_FILDES").toString();
			
			StringBuilder sb = new StringBuilder();
			URL url = new URL("http://" + esServerIps + ":"+esServerPorto+"/" + esIndex);
			String json = "	{\"settings\":{\"max_result_window\": \"1000000\",\"number_of_shards\":5,\"number_of_replicas\":0},\"mappings\":{\"doc\":{\"properties\":{";
			json += filed;
			json += "}}}}}";
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
			con.setRequestProperty("Accept", "application/json");
			con.setRequestMethod("PUT");

			OutputStream os = con.getOutputStream();
			os.write(json.getBytes("UTF-8"));
			os.flush();
			os.close();
			int HttpResult = con.getResponseCode();
			if (HttpResult == HttpURLConnection.HTTP_OK) {
				BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
				String line = null;
				while ((line = br.readLine()) != null) {
					sb.append(line + "\n");
				}
				br.close();
			} else {
				return InvokeResult.failure("创建ES通道失败！");
			}
			return InvokeResult.success("创建索引成功，索引名称为【"+esIndex+"】");
		} catch (Exception e) {
			e.printStackTrace();
			return InvokeResult.failure(e.getMessage());
		}
	}
	/**
	 *  @Discription：删除索引
	 *  @Author： liuhongjun
	 *  @return
	 *  @Date:2018年5月11日上午10:41:55
	 */
	@RequestMapping("/deleteEsIndex")
	@ResponseBody
	public InvokeResult deleteEsIndex(String esIndex) {
		try {
			Map<String,Object> map=getEsInfo(esIndex);
			URL url = null;
			StringBuilder sb = new StringBuilder();
			//服务器ip
			if(map!=null) {
				String esServerIps =map.get("ES_SERVERIP")==null?"127.0.0.1":map.get("ES_SERVERIP").toString();
				//ES服务端口，默认9200
				String esServerPorto =map.get("ES_CREATE_PORT")==null?"9200":map.get("ES_CREATE_PORT").toString();
				url = new URL("http://" + esServerIps + ":"+esServerPorto+"/" + esIndex);
			}else {
				url = new URL("http://127.0.0.1:9200/" + esIndex);
			}
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
			con.setRequestProperty("Accept", "application/json");
			con.setRequestMethod("DELETE");
			OutputStream os = con.getOutputStream();
			os.flush();
			os.close();
			int HttpResult = con.getResponseCode();
			if (HttpResult == HttpURLConnection.HTTP_OK) {
				BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
				String line = null;
				while ((line = br.readLine()) != null) {
					sb.append(line + "\n");
				}
				br.close();
				return InvokeResult.success("【"+esIndex+"】已删除");
			} else {
				System.out.println(con.getResponseMessage());
				return InvokeResult.success("【"+esIndex+"】不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return InvokeResult.failure(e.getMessage());
		}
	}
	/**
	 *  @Discription：获取es服务
	 *  @Author： liuhongjun
	 *  @param esindex
	 *  @return
	 *  @Date:2018年10月30日下午3:51:41
	 */
	public Map<String,Object> getEsInfo (String esindex){
		List<Map<String,Object>> list =esRestTempFacade.getEsInfo(esindex);
		Map<String,Object> map= null;
		if(!list.isEmpty()) {
			map=list.get(0);
		}
		return map;
	}
}
